package com.dxl.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxl.system.mapper.TypeMapper;
import com.dxl.system.mapper.NoteMapper;
import com.dxl.system.service.TypeService;
import com.dxl.system.model.Type;
import com.dxl.base.Result;
import com.dxl.base.Results;
import com.dxl.system.model.Note;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <b> 文件类型服务接口实现类
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-10-03    <br>Author : dxl
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper,Type> implements TypeService {

    @Autowired(required = false)
    private TypeMapper typeMapper;
    @Autowired(required = false)
    private NoteMapper noteMapper;

    /**
     * <b> 插入文件类型
     * </b><br><br><i>Description</i> : 不能为空：文件类型后缀-type_suffix 打开文件的程序路径-exe_path ；不能重复：文件类型后缀-type_suffix  
     * @param type Type
     * @return com.dxl.base.Result Type
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result addOne(Type type) {
        Result result = isTypeAddable(type);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        if(type.getTypeSuffix().contains(".")){
            type.setTypeSuffix(type.getTypeSuffix().replace(".",""));
        }
        if(!"是".equals(type.getIsHtml())){
            type.setIsHtml("否");
        }
        typeMapper.insert(type);
        return Results.successWithData(type);
    }

    /**
     * <b> 删除文件类型
     * </b><br><br><i>Description</i> : 
     * @param typeIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result delByIds(List<Integer> typeIds) {
        for(Integer id : typeIds){
            QueryWrapper<Note> wrapper = new QueryWrapper<>();
            wrapper.eq("type_id",id);
            List<Note> noteList = noteMapper.selectList(wrapper);
            if(noteList.size() > 0){
                return Results.failure("系统中已有该类型零件，不能删除:(");
            }
        }
        int num = typeMapper.deleteBatchIds(typeIds);
        return Results.successWithData(num);
    }


    /**
     * <b> 编辑文件类型
     * </b><br><br><i>Description</i> : 不能重复：文件类型后缀-type_suffix 
     * @param type Type
     * @return com.dxl.base.Result Type
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    @Transactional
    public Result editOne(Type type) {
        Result result = isTypeEditable(type);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        Type typeOld = typeMapper.selectById(type);
        //同步note表
        if(typeOld != null &&
          ((type.getTypeSuffix() != null && !type.getTypeSuffix().equals(typeOld.getTypeSuffix())))){
            Note note = new Note();
            note.setTypeSuffix(type.getTypeSuffix());
            QueryWrapper<Note> noteWrapper = new QueryWrapper<>();
            noteWrapper.eq("type_id",type.getId());
            noteMapper.update(note,noteWrapper);
        }
        if(!"是".equals(type.getIsHtml())){
            type.setIsHtml("否");
        }
        typeMapper.updateById(type);
        return Results.successWithData(type);
    }

    /**
     * <b> ID查询文件类型
     * </b><br><br><i>Description</i> : 
     * @param typeId Integer
     * @return com.dxl.base.Result: Type
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result getById(Integer typeId) {
        Type type = typeMapper.selectById(typeId);
        return Results.successWithData(type);
    }

    /**
     * <b> 条件查询文件类型
     * </b><br><br><i>Description</i> : 
     * @param type Type
     * @return com.dxl.base.Result List: Type
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result getList(Type type) {
        QueryWrapper<Type> typeWrapper = new QueryWrapper<Type>(type);
        List<Type> typeList = typeMapper.selectList(typeWrapper);
        return Results.successWithData(typeList);
    }

    /**
     * <b> 分页条件查询文件类型
     * </b><br><br><i>Description</i> : 
     * @param type Type, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Type
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result getListByPage(Type type, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Type> typeList = (List<Type>) getList(type).getData();
        PageInfo<Type> pageInfo = new PageInfo<Type>(typeList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 文件类型能否新增
     * </b><br><br><i>Description</i> : 不能为空：文件类型后缀-type_suffix 打开文件的程序路径-exe_path ；不能重复：文件类型后缀-type_suffix  
     * @param type Type
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result isTypeAddable(Type type) {

        if(type.getTypeSuffix() == null || "".equals(type.getTypeSuffix())){
            return Results.successWithData(false,"文件类型后缀 不能为空:(");
        }

        if(type.getTypeSuffix() != null){
        QueryWrapper<Type> typeWrapper = new QueryWrapper<>();
        typeWrapper.eq("type_suffix",type.getTypeSuffix());
        List<Type> typeList = typeMapper.selectList(typeWrapper);
            if(typeList.size() > 0){
                return Results.successWithData(false,"文件类型后缀 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
     * <b> 文件类型能否编辑
     * </b><br><br><i>Description</i> : 不能重复：文件类型后缀-type_suffix 
     * @param type Type
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result isTypeEditable(Type type) {

        if(type.getTypeSuffix() != null){
            QueryWrapper<Type> typeWrapper1 = new QueryWrapper<>();
            typeWrapper1.eq("id",type.getId()).eq("type_suffix",type.getTypeSuffix());
            List<Type> typeList1 = typeMapper.selectList(typeWrapper1);
            QueryWrapper<Type> typeWrapper2 = new QueryWrapper<>();
            typeWrapper2.eq("type_suffix",type.getTypeSuffix());
            List<Type> typeList2 = typeMapper.selectList(typeWrapper2);
            if(!((typeList1.size() == 1 && typeList2.size() == 1) ||(typeList1.size() == 0 && typeList2.size() == 0))){
                return Results.successWithData(false,"文件类型后缀 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }
}
